using System;
using System.Management;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using Microsoft.Win32;
using System.IO;
using System.Data.OleDb;
using Dll.Essences;
using Dll.DAL;
using System.Data;

namespace Dll
{
    public class BasePath
    { 
      public static OleDbConnection connect
        {
            get
            {
                return DataBaseConnection.GetConnection;
            }
        }

      public static string getConnectionFromDataSource(string dataSource)
      {

          return "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dataSource + ";Jet OLEDB:Database Password=sn87071331;";
      }      
    }

    public class DataBaseConnection
    {
        static OleDbConnection connect;
        public static string s;
        public static string getConnection()
        {
            try
            {
                connect.Close();
            }
            catch { }
            connect = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + s + "\\base\\base.base;Jet OLEDB:Database Password=sn87071331;");
            connect.Open();
            return "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + s + "\\base\\base.base;Jet OLEDB:Database Password=sn87071331;";
        }

        public static OleDbConnection GetConnection
        {
            get
            {
               
                try
                {
                    if (connect == null || connect.State == ConnectionState.Closed)
                        getConnection();
                }
                catch
                {
                    getConnection();
                }
                return connect;
            }
        }
    }

    public class GoodsFilt
    {
        public string name;
        public int producersID;
        public int ndsTypeID;
        public int morion;
        public int goodsGroupID;

        public GoodsFilt()
        {
            name = "";
            producersID = -1;
            ndsTypeID = -1;
            morion = 0;
            goodsGroupID = -1;
        }

    }

    public struct OrderFilt
    {
        public DateTime date1;
        public DateTime date2;
        public string name;
        public int producersID;
        public int providersID;
        public int structUnitID;
        public int orderID;
    }

    public class BindFilt
    {
        public string gName;
        public string gpName;
        public int provID;

        public BindFilt()
        {
            gName = "";
            provID=-1;
            gpName = "";
        }
    }

    public class SerialNumber
    {
      public  static string GetCodeSHA1(string s)
      {
          if (s == null)
              s = "";
            byte[] b;
            byte[] b2;
            SHA1 m = new SHA1CryptoServiceProvider();
            b = ASCIIEncoding.Default.GetBytes(s);
            b2 = m.ComputeHash(b);
            return BitConverter.ToString(b2);
        }

        public static string GetCodeMD5(string s)
        {
            byte[] b;
            byte[] b2;
            MD5 m = new MD5CryptoServiceProvider();
            b = ASCIIEncoding.Default.GetBytes(s);
            b2 = m.ComputeHash(b);
            return BitConverter.ToString(b2);
        }

        public static string serialNumber()
        {
            ManagementObjectSearcher s = new ManagementObjectSearcher("select *from win32_physicalMedia");

            foreach (ManagementObject wmi in s.Get())
            {
                return wmi["serialNumber"].ToString();
            }
            return "";
        }
    }

    public class PriceGuideRegistry
    {
        public static RegistryKey GetPriceGuide()
        {
            RegistryKey CurentUser = Registry.CurrentUser;
            CurentUser = CurentUser.OpenSubKey("Software", true);
            RegistryKey PriceGuide = CurentUser.OpenSubKey("Price Guide", true);
            if (PriceGuide == null)
            {
                PriceGuide = CurentUser.CreateSubKey("Price Guide");
                PriceGuide.SetValue("UserName", "");
            }
            return PriceGuide;
        }

        public static int GetShowFilter()
        {
            //RegistryKey rKey = GetPriceGuide();
            //Object ShowFilter = rKey.GetValue("ShowFilter");
            //if (ShowFilter == null)
            //{
            //    rKey.SetValue("ShowFilter", 1);
            //    return 1;
            //}

            //try
            //{
            //    int t = Convert.ToInt32(ShowFilter);
            //    if (t > 1 || t < 0)
            //    {
            //        rKey = PriceGuideRegistry.GetPriceGuide();
            //        rKey.SetValue("ShowFilter", 1);
            //        return 1;
            //    }
            //    else
            //        return t;
            //}
            //catch
            //{
            //    rKey = PriceGuideRegistry.GetPriceGuide();
            //    rKey.SetValue("ShowFilter", 1);
            //}
            return 1;

        }

        public static void SetShowFilter(int n)
        {
            //RegistryKey rKey = GetPriceGuide();
            //rKey.SetValue("ShowFilter", n);
        }

        public static void SetDiffFont(double n)
        {
            RegistryKey rKey = GetPriceGuide();
            rKey.SetValue("DiffFont", n);
        }

        public static float GetDiffFont()
        {
            float deffaultFont = 8.25f;
           RegistryKey rKey = GetPriceGuide();
           Object DiffFont = rKey.GetValue("DiffFont");
           if (DiffFont == null)
           {
               rKey.SetValue("DiffFont", deffaultFont);
               return 8.25f;
           }
           try
           {
               float d = (float)Convert.ToDouble(rKey.GetValue("DiffFont"));
               if (d > 72 || d < 5)
                   SetDiffFont(deffaultFont);
               else
               return d;
           }
           catch
           {
               SetDiffFont(deffaultFont);
           }
           return deffaultFont;

        }

        public static void SetMdiBar()
        {
            RegistryKey CurentUser = Registry.CurrentUser;
            CurentUser = CurentUser.OpenSubKey("Software", true);
            RegistryKey Softgroup = CurentUser.OpenSubKey("Softgroup", true);
            if (Softgroup == null)
                Softgroup = CurentUser.CreateSubKey("Softgroup");               
            RegistryKey NetMDIBar = Softgroup.OpenSubKey("NetMDIBar", true);
            if (NetMDIBar == null)
                NetMDIBar = Softgroup.CreateSubKey("NetMDIBar");
            NetMDIBar.SetValue("LicenseKey", "IXAJKSDJPDHBPRAU9OIYLZGEE");
            NetMDIBar.SetValue("LicenseName", "TEAM Lz0");
            NetMDIBar.SetValue("LicenseUser", "Lz0@Softgroup.com");
            NetMDIBar.Close();
        }
    }

    public class PriceInf
    {
        public PriceInf()
        {
            goods = null;
            producer = null;
            price = -1;
            nds = null;
            nextPrice = -1;
            code = null;
            morionCode = null;
            term = null;
            morion = -1;
        }

        public string goods;
        public string producer;
        public double price;
        public string nds;
        public double nextPrice;
        public string code ;
        public string morionCode ;
        public string term;
        public int morion;
     
    }

    public class ProvidersConnection
    {
        private static Dictionary<int, OleDbConnection> ProviderConnection = new Dictionary<int, OleDbConnection>();

        public static void AddConnection(int provID)
        {
            if (ProviderConnection.ContainsKey(provID))
            {
                try
                {
                    ProviderConnection[provID].Close();
                }
                catch
                {

                }
            }
            Providers p = ProvidersDAL.GetRrovidersByID(provID);
            OleDbConnection connect = new OleDbConnection();
            FileInfo file = new FileInfo(p.Path);
            string sheet = "";
            if (file.Extension.ToLower().IndexOf("xls") != -1)
            {
                //sheet = p.Sheet + "$";
                sheet += "A" + p.StartRowNumber + ":IV15000";
                connect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + p.Path;
                connect.ConnectionString += ";Extended Properties=\"Excel 3.0;HDR=Yes;IMEX=1\"";
            }
            else
            {
                sheet = p.Sheet;
                connect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + file.Directory;
                connect.ConnectionString += ";Extended Properties=dBASE IV";
            }
            connect.Open();
            ProviderConnection[provID] = connect;
        }

        public static OleDbConnection GetConnection(int provID)
        {
            try
            {
                if (ProviderConnection.ContainsKey(provID))
                {
                    try
                    {
                        if (ProviderConnection[provID] != null && ProviderConnection[provID].State == ConnectionState.Open)
                            return ProviderConnection[provID];
                    }
                    catch
                    {
                        try
                        {
                            ProviderConnection[provID].Close();
                        }
                        catch { }
                    
                    }
                }
                OleDbConnection connect = new OleDbConnection();
                Providers p = ProvidersDAL.GetRrovidersByID(provID);
                FileInfo file = new FileInfo(p.Path);
                string sheet = "";
                if (file.Extension.ToLower().IndexOf("xls") != -1)
                {
                    //sheet = p.Sheet + "$";
                    sheet += "A" + p.StartRowNumber + ":IV15000";
                    connect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + p.Path;
                    connect.ConnectionString += ";Extended Properties=\"Excel 5.0;HDR=Yes;IMEX=1\"";
                }
                else
                {
                    sheet = p.Sheet;
                    connect.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + file.Directory;
                    connect.ConnectionString += ";Extended Properties=dBASE IV";
                }
                ProviderConnection[provID] = connect;

                ProviderConnection[provID].Open();
                
                return ProviderConnection[provID];
            }
            catch
            {
                return null;
            }
        }

        public static void CloseConnection(int provID)
        {
            if (ProviderConnection.ContainsKey(provID))
            {
                try
                {
                    ProviderConnection[provID].Close();
                    ProviderConnection.Remove(provID);
                }
                catch { }
            }
        }

        public static bool ExistsConnection(int provID)
        {
            return ProviderConnection.ContainsKey(provID);
        }
    }

    public class StringFunction
    {
        public static bool isGoodsName(string name)
        {
            if (name == "")
                return false;
            for (int i = 0; i < name.Length; i++)
                if (name[i] != '%' && name[i] != ' ' && name[i] != '\t')
                    return true;
            return false;
        }
    }
}
